package com.suyang.syt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyang.syt.dao.interfaces.BannerDao;
import com.suyang.syt.dao.interfaces.CitysDao;
import com.suyang.syt.dao.interfaces.PicDao;
import com.suyang.syt.model.Banner;
import com.suyang.syt.model.Citys;
import com.suyang.syt.model.Pic;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.BannerDTO;
import com.suyang.syt.model.dto.PagerEasyUI;
import com.suyang.syt.model.dto.app.AppPicDTO;
import com.suyang.syt.service.interfaces.BannerService;
import com.suyang.syt.util.AppUtil;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;

@Service
public class BannerServiceImpl implements BannerService {
	@Autowired
	private BannerDao bannerDao;
	@Autowired
	private PicDao picDao;
	@Autowired
	private CitysDao citysDao;

	@Override
	public PagerEasyUI<BannerDTO> listByEasyUI(int page, int rows, String sort, String order, long objId, int flag, int opterType, String createDate1, String createDate2) throws Exception {
		StringBuilder hql = new StringBuilder(" from Banner b where flag=");
		hql.append(flag).append(" and objId=").append(objId);
		if (opterType >= 0) {
			hql.append(" and b.opterType = ").append(opterType);
		}
		if (EmptyUtil.isNotEmpty(createDate1)) {
			hql.append(" and b.createTime >= '").append(createDate1).append("' ");
		}
		if (EmptyUtil.isNotEmpty(createDate2)) {
			hql.append(" and b.createTime <= '").append(createDate2).append("' ");
		}
		if (EmptyUtil.isNotEmpty(sort)) {
			hql.append(" order by b.").append(sort);
		}
		if (EmptyUtil.isNotEmpty(order)) {
			hql.append(" ").append(order);
		}
		PagerEasyUI<BannerDTO> easyUIPager = new PagerEasyUI<BannerDTO>();
		List<Banner> banners = bannerDao.findByEasyUI(hql.toString(), page, rows);
		List<BannerDTO> bannerDTOS = new ArrayList<BannerDTO>();
		BannerDTO bannerDTO = null;
		for (Banner banner : banners) {
			bannerDTO = new BannerDTO(banner);
			bannerDTOS.add(bannerDTO);
		}
		easyUIPager.setRows(bannerDTOS);
		easyUIPager.setTotal((Long) bannerDao.queryByHql(" select count(*) " + hql.substring(0, hql.indexOf("order by"))));
		return easyUIPager;
	}

	@Override
	public AjaxObj add(BannerDTO bannerDTO) throws Exception {
		long objId = bannerDTO.getObjId();
		if (bannerDTO != null && EmptyUtil.isNotEmpty(bannerDTO.getName())) {
			Banner banner = new Banner();
			int flag = bannerDTO.getFlag();
			switch (flag) {
			case 1:
				if (objId < 2) {
					return new AjaxObj(0, "城市ID错误!");
				}
				if (bannerDao.countBannersBy(flag, objId) >= 5) {
					return new AjaxObj(0, "每个城市只能保留5个Banner");
				}
				Citys citys = citysDao.load(objId);
				if (citys == null) {
					return new AjaxObj(0, "城市信息加载错误,请联系管理员!");
				}
				banner.setObjId(citys.getId());
				banner.setObjName(citys.getText());
				break;
			case 2:
				banner.setObjId(0);
				banner.setObjName("");
				break;
			}
			banner.setFlag(bannerDTO.getFlag());
			banner.setOpterType(bannerDTO.getOpterType());
			banner.setCreateName(bannerDTO.getCreateName());
			banner.setCreateTime(bannerDTO.getCreateTime());
			banner.setName(bannerDTO.getName());
			bannerDao.add(banner);
			// 保证轮播的唯一性
			bannerDao.setStatus(banner.getId(), objId, bannerDTO.getFlag());
			return new AjaxObj(1, "Banner添加成功!");
		}
		return new AjaxObj(0, "Banner添加失败!");
	}

	@Override
	public AjaxObj deletes(String ids) throws Exception {
		List<Pic> list = picDao.findByBids(ids);
		if (EmptyUtil.isNotEmpty(list)) {
			picDao.deleteByBids(ids);
		}
		bannerDao.deletes(ids);
		return new AjaxObj(1, "删除成功!");
	}

	@Override
	public AjaxObj update(BannerDTO bannerDTO) throws Exception {
		if (bannerDTO != null && EmptyUtil.isNotEmpty(bannerDTO.getName())) {
			Banner banner = bannerDao.load(bannerDTO.getId());
			if (banner == null) {
				return new AjaxObj(0, "加载Banner信息失败,请联系管理员!");
			}
			banner.setModifyName(bannerDTO.getModifyName());
			banner.setModifyTime(bannerDTO.getModifyTime());
			banner.setOpterType(bannerDTO.getOpterType());
			banner.setName(bannerDTO.getName());
			if (banner.getStatus() != bannerDTO.getStatus()) {
				if (bannerDTO.getStatus() == 1) {
					// 保证轮播的唯一性
					bannerDao.setStatus(banner.getId(), bannerDTO.getObjId(), bannerDTO.getFlag());
				}
			}
			bannerDao.update(banner);
			return new AjaxObj(1, "更新成功！");
		}
		return new AjaxObj(0, "更新失败！");
	}

	@Override
	public AjaxObj updatePic(String bid, String orderIndex, String picPath) throws Exception {
		if (EmptyUtil.isNotEmpty(bid)) {
			Banner banner = bannerDao.load(Long.parseLong(bid));
			if (banner == null) {
				return new AjaxObj(0, "加载Banner信息失败,请联系管理员!");
			}
			picDao.deleteByBids(bid);
			if (EmptyUtil.isNotEmpty(picPath)) {
				String[] picPathArr = picPath.split(",");
				if (picPathArr != null && picPathArr.length > 4) {
					return new AjaxObj(0, "目前每个机构只支持4张图片!");
				}
				Pic tempPic = null;
				for (String path : picPathArr) {
					tempPic = new Pic();
					tempPic.setBid(banner.getId());
					tempPic.setCreateTime(DateFormat.dateToString(new Date()));
					tempPic.setPath(path);
					tempPic.setStatus(Integer.parseInt(orderIndex));
					picDao.add(tempPic);
				}
			}
			return new AjaxObj(1, "更新成功！");
		}
		return new AjaxObj(0, "更新失败！");
	}

	@Override
	public AjaxObj findPic(long cid) throws Exception {
		List<AppPicDTO> datas = new ArrayList<AppPicDTO>();
		AppPicDTO picDTO = null;
		List<Pic> pics = null;
		if (cid == 3) {
			pics = picDao.findByBanner(1);
		} else {
			List<Banner> list = bannerDao.list("from Banner b where b.flag=1 and b.status=1 and b.objId=?", cid);
			if (list != null && list.size() == 1) {
				pics = picDao.findByBanner(list.get(0).getId());
			} else {
				pics = picDao.findByBanner(1);
			}
		}
		if (pics != null && pics.size() >= 3) {
			for (Pic pic : pics) {
				picDTO = new AppPicDTO(pic);
				picDTO.setPath(AppUtil.getUploadPath() + "/banner/" + pic.getPath());
				datas.add(picDTO);
			}
			return new AjaxObj(1, "轮播图片加载成功!", datas);
		}
		return null;
	}

	@Override
	public Banner load(long bid) throws Exception {
		return bannerDao.load(bid);
	}

	@Override
	public List<Banner> list(String ids) throws Exception {
		String hql = " from Banner b where b.id in(" + ids + ")";
		return bannerDao.list(hql);
	}

}
